package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.MySendQaAdapter;
import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.listener.IQaListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.MySendQaManager;
import com.wbao.dianniu.manager.QaDataChangeManager;
import com.wbao.dianniu.manager.QaDataChangeManager.IQaDataListener;
import com.wbao.dianniu.utils.Utils;

/**
 * 我发的问答
 * 
 * @author yejianjun
 *
 */
public class SendQaFragment extends Fragment implements IQaListener,OnClickListener, IQaDataListener {

	private LinearLayout noDataLayout;
	private Button sendBtn;
	private PullToRefreshListView pullListView;
	private ListView mListView;
	private MySendQaManager manager;
	private int TYPE = 3;
	private int currentPage = 0;
	private final int PAGE_COUNTS = 10;
	private MySendQaAdapter adapter;
	private List<QaResponse> mList;
	private Dialog dialog;
	
	public static SendQaFragment instance(Context context){
		SendQaFragment view = new SendQaFragment();
		return view;
	}
	
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_send_qa, null);
		adapter =new MySendQaAdapter(view.getContext(),1);
		QaDataChangeManager.getInstance().addQaChangeListener(this);
		initView(view);
		requestData();
		return view;
	}
	
	private void requestData(){
		manager = new MySendQaManager(getActivity());
		manager.addQaListener(this);
		reqData(currentPage);
	}
	
	private void initView(View view){
		noDataLayout = (LinearLayout) view.findViewById(R.id.no_data_layout);
		sendBtn = (Button) view.findViewById(R.id.send_qa_button);
		
		pullListView = (PullToRefreshListView) view.findViewById(R.id.list_send_qa);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		mListView = pullListView.getRefreshableView();
		registerForContextMenu(mListView);
		mListView.setAdapter(adapter);
		sendBtn.setOnClickListener(this);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Utils.intoQaDetailActivity(getContext(), (QaResponse)adapter.getItem(position-1));
			}
		});
	}
	
	OnRefreshListener onRefreshListener = new OnRefreshListener() {
		@Override
		public void onRefresh() {
			// 上拉刷新 加载请求数据
			pullUpRequest();
		}
	};
	// 上拉刷新数据请求
	private void pullUpRequest() {
		// 没有搜索结果 或者数据为空 上拉没有效果
		++currentPage;
		reqData(currentPage);
	}
	private  void reqData(int page){
		manager.reqData(GlobalContext.accountId, "", TYPE, page * PAGE_COUNTS , PAGE_COUNTS);
	}

	@Override
	public void onQaFailure(int errorCode, String errorMsg) {
		pullListView.onRefreshComplete(false,false);
	}

	@Override
	public void onQaSuccess(List<QaResponse> list) {
		pullListView.onRefreshComplete(false,false);
		if (null != list) {
			if (0 != currentPage) {
				adapter.addDataList(list);
			} else {
				adapter.addDataList(list);
			}
			mList = adapter.getList();
			adapter.notifyDataSetChanged();
			if (mList.size() == 0) {
				noDataLayout.setVisibility(View.VISIBLE);
			} else {
				noDataLayout.setVisibility(View.GONE);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.send_qa_button:
			if (checkLevel()) {
				Intent ask = new Intent();
				ask.setClass(getActivity(), AskActivity.class);
				startActivity(ask);
			} else {
				showAuthLevel("您还未完成认证，请先认证！");
			}
			break;

		default:
			break;
		}
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("SendQAFragment");
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("SendQAFragment"); 
	}

	public boolean checkLevel() {
		boolean ret = false;
		if (GlobalContext.isPerfectData) {
			// 提示用户完成认证 必须达到一级认证才能提问
			ret = true;
		}
		return ret;
	}
	
	/**
	 * 显示认证弹出框
	 */
	private void showAuthLevel(String msg) {
		dialog = new Dialog(getActivity(), R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.CENTER);
		View view = LayoutInflater.from(getActivity()).inflate(R.layout.custom_message_dialog, null);
		Button okBtn = (Button) view.findViewById(R.id.custom_msg_ok);
		Button cancelBtn = (Button) view.findViewById(R.id.custom_msg_cancel);
		TextView message = (TextView) view.findViewById(R.id.custom_msg_message);
		message.setText(msg);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				// 跳转到认证界面
				Intent intent = new Intent();
				intent.setClass(getActivity(), PerfectInfoActivity.class);
				startActivity(intent);
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		QaDataChangeManager.getInstance().removeQaChangeListener(this);
		if (null != dialog && dialog.isShowing()) {
			dialog.dismiss();
		}
		if(null != manager){
			manager.removeQaListener(this);
		}
	}

	@Override
	public void removeOneData(int questId) {
		adapter.removeOneData(questId);
		mList = adapter.getList();
		if(0 == mList.size()){
			noDataLayout.setVisibility(View.VISIBLE);
		}else{
			noDataLayout.setVisibility(View.GONE);
		}
	}

	@Override
	public void priseNotify(int questId, int isGood, int goodCounts) {
		// TODO Auto-generated method stub
		adapter.priseNotify(questId, isGood, goodCounts);
	}

	@Override
	public void replyNotify(int questId, int answerCounts) {
		// TODO Auto-generated method stub
		adapter.replyNotify(questId,answerCounts);
	}

	@Override
	public void addOneData(QaResponse addData) {
		// TODO Auto-generated method stub
		noDataLayout.setVisibility(View.GONE);
		List<QaResponse> list = new ArrayList<QaResponse>();
		list.add(addData);
		adapter.addDataList(list);
		adapter.notifyDataSetChanged();
	}
	
}
